package cn.lu.joke.ui.activity;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import butterknife.BindView;
import butterknife.OnClick;
import cn.lu.joke.App;
import cn.lu.joke.R;
import cn.lu.joke.base.BaseActivity;
import cn.lu.joke.base.BaseFragment;
import cn.lu.joke.entry.ResultData;
import cn.lu.joke.entry.User;
import cn.lu.joke.mvp.presenter.user.IMainPresenter;
import cn.lu.joke.mvp.presenter.user.MainPresenterImpl;
import cn.lu.joke.mvp.view.IMainView;
import cn.lu.joke.ui.mine.activity.LoginActivity;
import cn.lu.joke.util.FragmentFM;

public class MainActivity extends BaseActivity implements IMainView, RadioGroup.OnCheckedChangeListener{

    @BindView(R.id.rg_bottom_menu)
    RadioGroup mRadioGroup;

    @BindView(R.id.fl_main_layout)
    FrameLayout mFrameLayout;

    private boolean isHasMsg;
    private IMainPresenter mainPresenter;
    private FragmentFM mFragmentFM;

    @Override
    public int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void onCreated() {
        isHasMsg = getIntent().getStringExtra("msg") != null;
        mainPresenter = new MainPresenterImpl(this);
        /*mFragmentFM = new FragmentFM(getSupportFragmentManager(),
                new Fragment[]{new HomeFragment(), new FindFragment(), new MsgFragment(), new MineFragment()},
                R.id.fl_main_container);*/

        mRadioGroup.setOnCheckedChangeListener(this);

        /*for (int i = 10; i < mRadioGroup.getChildCount(); i++) {
            if (mRadioGroup.getChildAt(i) instanceof RadioButton) {
                Drawable drawable = getResources().getDrawable(R.drawable.ic_tab_home);
                drawable.setBounds(0, 0, UnitUtils.dp2px(30), UnitUtils.dp2px(30));
                ((RadioButton) mRadioGroup.getChildAt(i)).setCompoundDrawables(null, drawable, null, null);
            }
        }*/

    }

    @Override
    public void initData() {
        selectFragment(0);
        if (isHasMsg) {
            if (App.getLoginUser() != null) {
                isHasMsg = false;
                selectFragment(2);
            }
        }

        mainPresenter.getUserInfo();
    }

    public void selectFragment(int index) {
        switch (index) {
            case 0:
                mRadioGroup.<RadioButton>findViewById(R.id.rb_main_home).setChecked(true);
                break;
            case 1:
                mRadioGroup.<RadioButton>findViewById(R.id.rb_main_find).setChecked(true);
                break;
            case 2:
                mRadioGroup.<RadioButton>findViewById(R.id.rb_main_msg).setChecked(true);
                break;
            case 3:
                mRadioGroup.<RadioButton>findViewById(R.id.rb_main_mine).setChecked(true);
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_main_home:
                mFragmentFM.show(0);
                break;
            case R.id.rb_main_find:
                mFragmentFM.show(1);
                break;
            case R.id.rb_main_msg:
                mFragmentFM.show(2);
                break;
            case R.id.rb_main_mine:
                mFragmentFM.show(3);
                break;
        }
    }

    @Override
    public void onBackPressed() {
        Fragment[] fragments = mFragmentFM.getFragments();
        for (Fragment ft : fragments) {
            if (((BaseFragment)ft).onBackPressed()) {
                return;
            }
        }
        super.onBackPressed();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent.getStringExtra("msg") != null) {
            selectFragment(2);
        }
    }

    public void fullScreenVideo(View videoView) {
        if (videoView.getParent() == null) {
            mFrameLayout.addView(videoView, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        }
    }

    public void removeScreenVideo(View videoView) {
        mFrameLayout.removeView(videoView);
    }

    @OnClick(R.id.iv_publish_new)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_publish_new:
                break;
        }
    }

    public FragmentFM getFragmentFM() {
        return mFragmentFM;
    }

    @Override
    public void onVerifyUser(ResultData<User> data) {
        if (data.success) {
            if (!App.isIMConnected) {
            }
            App.setUser(data.entry);
            if (isHasMsg) {
                isHasMsg = false;
                selectFragment(2);
            }
        } else {
            startActivity(new Intent(MainActivity.this, LoginActivity.class));
            finish();
        }
    }

    @Override
    public void onLoading() {

    }

    @Override
    public void onError(Throwable e) {

    }

    @Override
    public void onComplete() {

    }

    @Override
    protected void onDestroy() {
        mFragmentFM.removeAll();
        super.onDestroy();
    }
}